home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-04 | 3.3 KB | 157 lines | [TEXT/CWIE] |
- // MADE - Macintosh Application Development Essentials
- // ---------------------------------------------------
-
- // (c) Gideon Greenspan, Sig Software - June 1997 - http://www.kagi.com/gdg/
-
- // These files can only be used for experimental standalone purposes. To obtain
- // fully commented code, and licenses for standalone, shareware, internal and
- // commercial usage, run the enclosed Register application.
-
- // Essential Memory.c
- //
- // Memory allocation, deallocation and initialisation.
- //
- // Version 1.0.0 - 10th November 1996
- // Version 1.0.1 - 4th June 1997
- // Fixed macro, function names
-
- #include "Essential Headers.h"
- #include "Essential Prototypes.h"
-
- Error CreateHeapSpace(Size required)
- {
- Error error=0;
- Size allocate;
-
- allocate=required+Emergency_Memory_Reserve;
-
- if (MaxBlock()<allocate) {
-
- PurgeMem(allocate);
- if (MaxBlock()<allocate) {
-
- MyFreeUpMemory(allocate);
-
- if (MaxBlock()<allocate)
- error=memFullErr;
- }
- }
-
- TestError(error);
- return error;
- }
-
- void* AllocPtr(Error* error, Size allocate)
- {
- void* pointer;
-
- *error=CreateHeapSpace(allocate);
- _i(*error)
-
- pointer=(void*)NewPtr(allocate);
- *error=TestMemError(pointer);
- _i(*error);
-
- #if Project_Under_Development && Initialise_Allocated_Memory
- InitialiseMemory(pointer, allocate);
- #endif
-
- return pointer;
- _e
- return 0;
- }
-
- void** AllocHandle(Error* error, Size allocate)
- {
- void** handle;
-
- *error=CreateHeapSpace(allocate);
- _i(*error)
-
- handle=(void**)NewHandle(allocate);
- *error=TestMemError(handle);
- _i(*error);
-
- #if Project_Under_Development
- #if Move_On_Handle_Allocation
- MoveHHi((Handle)handle);
- #endif
-
- #if Initialise_Allocated_Memory
- InitialiseMemory(*handle, allocate);
- #endif
- #endif
-
- return handle;
- _e
- return 0;
- }
-
- void DestroyPtr(void* pointer)
- {
- #if Project_Under_Development && Initialise_Allocated_Memory
- InitialiseMemory(pointer, GetPtrSize((Ptr)pointer));
- #endif
- DisposPtr((Ptr)pointer);
- }
-
- void DestroyHandle(void** handle)
- {
- #if Project_Under_Development && Initialise_Allocated_Memory
- InitialiseMemory(*handle, GetHandleSize((Handle)handle));
- #endif
- DisposHandle((Handle)handle);
- }
-
- #if Project_Under_Development
-
- #if Assert_Memory_Locking
-
- // I'm sure the next two routines could be written better with more knowledge of
- // Apple's System Software, but the programming guidelines don't offer such
- // information, so it's likely to change. Thus I do this the long way round.
-
- void LockHandleAssert(void** handle)
- {
- SignedByte statusBefore, statusAfter;
-
- statusBefore=HGetState((Handle)handle);
- HLock((Handle)handle);
- statusAfter=HGetState((Handle)handle);
-
- Assert(statusBefore!=statusAfter);
- }
-
- void UnlockHandleAssert(void** handle)
- {
- SignedByte statusBefore, statusAfter;
-
- statusBefore=HGetState((Handle)handle);
- HUnlock((Handle)handle);
- statusAfter=HGetState((Handle)handle);
-
- Assert(statusBefore!=statusAfter);
- }
-
- #endif
-
- #if Initialise_Allocated_Memory
-
- // This could be optimised by first filling with long words of 0xAAAAAAAA, but there's
- // little point as it's only used when the project is under development anyway.
-
- void InitialiseMemory(char* pointer, Size length)
- {
- char *trasher, *topAddress;
-
- trasher=pointer;
- topAddress=trasher+length;
-
- while (trasher<topAddress)
- *trasher++=0xAA;
- }
-
- #endif
-
- #endif
-